<style scoped>
  .weui-form-preview__hd label, .weui-form-preview__hd em {
    font-size: .9em !important;
  }
  .weui-loadmore_line .weui-loadmore__tips {
    background-color: #eee;
  }
  .rj-cell.max span{
    font-size: 16px;
    padding-right: 20px;
  }
  .rj-cell label {
    color: #333;
    font-size: 14px;
    width: 44%;
    display: inline-block;
    padding: 5px 3%;
    text-align: right;
  }
  .rj-cell span {
    font-size: 14px;
    color: #333;
    display: inline-block;
    width: 50%;
  }
  .rj-cell.note label{
    float: left;
  }
  .rj-cell.note span{
    width: 80%;
    display: inline-block;
  }
  .weui-cells {
    margin-top: 0;
  }
  .weui-cells_form {
    background: #fbfbfb;
  }
  .weui-cell {
    background: #fff;
  }
  #acts span {
    font-size: 14px;
  }
  .halfbtn {
    display: inline-block;
    width: 49%;
  }
  .weui-label{
    /*width: 130px;*/
    text-align: left;
  }

  .weui-select {
    height: auto;
    line-height: normal;
  }
  *{
    list-style:none;
  }
  #acts span {
    font-size: 14px;
  }
  input.time {
    border: none;
    font-size: 16px;
    padding-left: 10px;
  }

</style>
<template>
  <div class="sendcar">
    <div style="height: 50px; line-height: 50px; background: #f7f7f7; text-align: center; font-size: 16px; border-bottom: 1px solid #bdbbbc; position: relative">
      添加销售合同
      <a href="javascript:;" @click="$router.back(-1)" style="position: absolute; left: 10px; top: 8px">
        <img src="../../assets/back.png" style="width: 20px" />
      </a>
    </div>
    <div class="weui-cells weui-cells_form">
      <form action="/api/sellform/add" method="post" id="formid">


        <div class="cellgroup">

          <div class="weui-cell">
            <div class="weui-cell__hd"><label class="weui-label">合同名称</label></div>
            <div class="weui-cell__bd">
              <input class="weui-input" type="text"   id="name" name="name" placeholder="请填写合同名称" >
            </div>
          </div>
          <div class="weui-cell">
            <div class="weui-cell__hd"><label class="weui-label">甲方 </label></div>
            <div class="weui-cell__bd">
              <select class="weui-select" name="partaorgguid" id="partaorgguid" v-model="partaorgguid">
                <option value="" >请选择</option>
                <option    v-for="(item, index) in organizations" :key="item.guid" :value="item.guid">{{item.name}}</option>
              </select>
            </div>
          </div>
          <div class="weui-cell">
            <div class="weui-cell__hd"><label class="weui-label">乙方</label></div>
            <div class="weui-cell__bd">
              <select class="weui-select" name="partborgguid" id="partborgguid">
                <option value="" >请选择</option>
                <option    v-for="(item, index) in organizations" :key="item.guid" :value="item.guid" v-if="partaorgguid != item.guid">{{item.name}}</option>
              </select>
            </div>
          </div>

          <div class="weui-cell">
            <div class="weui-cell__hd"><label class="weui-label">开始时间</label></div>
            <div class="weui-cell__bd">
              <input type="text"   class="time" id="starttime" name="starttime" placeholder="请选择开始时间" />
            </div>
          </div>


          <div class="weui-cell">
            <div class="weui-cell__hd"><label class="weui-label">备注</label></div>
            <div class="weui-cell__bd">
              <input class="weui-input" type="text"  id="remark"   name="remark" placeholder="请填写合同备注" >
            </div>
          </div>

          <!--            <div class="weui-cell">-->
          <!--              <div class="weui-cell__hd"><label class="weui-label">状态</label></div>-->
          <!--              <div class="weui-cell__bd">-->
          <!--                <select class="weui-select" name="status">-->
          <!--                  <option value="1" >生效</option>-->
          <!--                  <option value="0" >失效</option>-->
          <!--                </select>-->
          <!--              </div>-->
          <!--            </div>-->
        </div>
        <ContractItem v-for="(item, index) in oitems" :key="index" :itemNo="item" :isLast="index+1 == oitems.length" :products="products" :specifications="specifications" @doAddItem="addItem(index + 1)" @doDelItem="delItem"></ContractItem>
        <div class="weui-btn-area">
          <a class="weui-btn weui-btn_primary halfbtn subbtn"   href="javascript:" @click="send">提交</a>
          <a class="weui-btn weui-btn_primary halfbtn"  href="javascript:" @click="$router.back(-1)" id="sub">返回</a>
        </div>
      </form>
    </div>
  </div>
</template>

<script>
  import ContractItem from "./ContractItem";
  export default {
    name: 'AddPurchasecontract',
    data () {
      return {
        openid: localStorage.openid,
        guid: '',
        type: 1,
        pickinglist: {},
        items: [],
        products: [],
        specifications: [],
        organizations:[],
        partaorgguid:"",
        oitems: [0]
      }
    },
    components: {ContractItem},
    created() {
      window.addEventListener('setItem', ()=> {
        this.openid = localStorage.getItem('openid');
      })
    },
    mounted () {
      $("#starttime").calendar();
      let that = this;
      this.guid = this.$route.query.guid;
      this.type = this.$route.query.type;
      //获取回显数据
      this.getApplyInfo(this.$route.query.guid);
    },
    computed: {

    },
    methods: {
      addItem: function(index) {
        this.oitems.push(index);
      },
      delItem: function() {
        this.oitems.pop();
      },
      getApplyInfo: function(guid) {
        this.axios({
          method: 'get',
          url: '/sales/Purchasecontract!getBaseInfo.action',
          params: {pickinglistid: guid}
        }).then((response) => {
          if (response.data.msg == 'success') {
            let data = response.data;
            console.log(data);
            this.products = data.products;
            this.specifications = data.spes;
            this.organizations = data.organization;
          } else {
            this.toastError("系统错误");
          }

        })
      },
      send: function() {
        if (!this.checkSubmit()) {
          return false;
        }
        //防止重复提交
        $('.subbtn').attr("disabled",true).css("pointer-events","none");

        let that  = this;
        this.axios({
          method: 'post',
          url: '/sales/Purchasecontract!save.action?openid=' + this.openid + '&pickinglistid=' + this.guid,
          params: this.serialize2Obj($("#formid").serializeArray())
        }).then(response => {
          //恢复提交按钮可点击
          $('.subbtn').attr('disabled', false).css("pointer-events","auto");

          if(response.data.msg == "success"){
            this.toastSuccess('操作成功!');
            this.$router.push({name: 'PurchasecontractList', query: {guid:that.pickinglist.guid}});

          }else if (response.data.msg == 'repeat'){
            this.toastText('合同明细中产品规格重复，请重新选择！');
          }else if (response.data.msg == 'itemEmpty'){
            this.toastText('合同明细价格和产品为空！');
          }else if (response.data.msg == 'error'){
            this.toastError('系统错误!');
          }
        }).catch(function (error) {
          //恢复提交按钮可点击
          $('.subbtn').attr('disabled', false).css("pointer-events","auto");
          this.toastError('系统错误');
        });

      },checkSubmit:function(){
        if(!this.isEmpty("name")){
          this.toastError('请输入合同名称');
          return  false;
        }
        if(!this.isEmpty("partaorgguid")){
          this.toastError('请选择甲方');
          return  false;
        }
        if(!this.isEmpty("partborgguid")){
          this.toastError('请选择乙方');
          return  false;
        }
        if(!this.isEmpty("starttime")){
          this.toastError('请选择开始时间');
          return  false;
        }
        return true;
      },
      isEmpty:function(domId){
        if($("#" + domId).val() == null ||  $("#" + domId).val() ==''){
          return false;
        }else{
          return true;
        }
      }

    }
  }


</script>
